package com.yxq.mdt.app.modules.login.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseActivity;
import com.yxq.mdt.app.modules.login.bean.UserBean;
import com.yxq.mdt.app.modules.login.model.LoginModel;
import com.yxq.mdt.app.modules.login.presenter.ILoginContract;
import com.yxq.mdt.app.modules.login.presenter.LoginPresenter;
import com.yxq.mdt.app.utils.StringUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author L
 * @time
 * @describe 忘记密码-验证手机号
 */
public class ForgetPasswordActivity extends BaseActivity<LoginPresenter> implements ILoginContract.View {

    @BindView(R.id.login_et_account)
    EditText accountEditText;
    @BindView(R.id.login_et_code)
    EditText codeEditText;
    @BindView(R.id.login_tv_code)
    TextView codeTextView;
    CountDownTimer countDownTimer;

    @Override
    public int getLayoutId() {
        return R.layout.activity_forget;
    }

    @Override
    public void initView() {
        mPresenter = new LoginPresenter(this, new LoginModel());
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

    }

    @OnClick({R.id.login_btn_login, R.id.login_tv_code})
    public void onClicked(View view) {
        switch (view.getId()) {
            case R.id.login_btn_login:
                if (accountEditText.getText().toString().length() == 0) {
                    ToastUtils.showShort("请输入手机号码");
                    return;
                }
                if (!StringUtils.isMobileExact(accountEditText.getText().toString())) {
                    ToastUtils.showShort("请输入合法的手机号码");
                    return;
                }
                if (codeEditText.getText().toString().length() == 0) {
                    ToastUtils.showShort("请输入验证码");
                    return;
                }
                Intent intent = new Intent(ForgetPasswordActivity.this, ResetPasswordActivity.class);
                intent.putExtra("phone", accountEditText.getText().toString());
                intent.putExtra("code", codeEditText.getText().toString());
                startActivity(intent);
                finish();
                break;
            case R.id.login_tv_code:
                verifyCode();
                break;
        }
    }

    private void verifyCode() {
        if (accountEditText.getText().toString().length() == 0) {
            ToastUtils.showShort("请输入手机号码");
            return;
        }
        if (!StringUtils.isMobileExact(accountEditText.getText().toString())) {
            ToastUtils.showShort("请输入合法的手机号码");
            return;
        }
        showLoadingDialog();
        mPresenter.verifyCode(accountEditText.getText().toString(), 2);
    }

    private void stopTimer() {
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
    }

    private void timer() {
        stopTimer();
        codeTextView.setEnabled(false);
        countDownTimer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long l) {
                codeTextView.setText((l / 1000) + "s");
            }

            @Override
            public void onFinish() {
                codeTextView.setText("获取验证码");
                codeTextView.setEnabled(true);
            }
        };
        countDownTimer.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopTimer();
    }

    @Override
    public void onLoginSuccess(UserBean userBean) {

    }

    @Override
    public void onLoginFailed(String error) {

    }

    @Override
    public void onVerifyCodeSuccess(String info) {
        hideLoadingDialog();
        ToastUtils.showShort("验证码已发送到你手机");
        timer();
    }

    @Override
    public void onVerifyCodeFailed(String error) {
        hideLoadingDialog();
    }

    @Override
    public void onRegisterSuccess(String userBean) {

    }

    @Override
    public void onRegisterFailed(String error) {

    }

    @Override
    public void onBindPhoneSuccess(String userBean) {

    }

    @Override
    public void onBindPhoneFailed(String error) {

    }
}
